package itbour.onetouchshow.utils.gsonanalysis;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;

import itbour.onetouchshow.bean.inching.noppt.InchingOutLineAllTypeV10Bean;
import itbour.onetouchshow.bean.inching.noppt.InchingOutLineImgBean;
import itbour.onetouchshow.bean.inching.noppt.InchingOutLineTableBean;
import itbour.onetouchshow.bean.inching.noppt.InchingOutLineTextOrShapeBean;
import itbour.onetouchshow.constants.AppConst;

/**
 * Created by zjl_d on 2017/11/24.
 * 微调轮廓的解析规则
 */

public class InchingOutLineAnalysisRules implements JsonDeserializer<InchingOutLineAllTypeV10Bean> {


    @Override
    public InchingOutLineAllTypeV10Bean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

        JsonObject jsonObject = json.getAsJsonObject();

        int ct = jsonObject.get("ct").getAsInt();
        Gson gson = new Gson();

        //通过ct来判断它到底是个什么类型
        //ct:609文字 603图形 602图片   文字和图形的结构基本相同 文字editable为1 图形603为0  610代表表格
        if (ct == AppConst.INCHING_IMAGEVIEW_TYPE) {
            InchingOutLineImgBean inchingTextsContourImageBean = gson.fromJson(json, InchingOutLineImgBean.class);
            return inchingTextsContourImageBean;
        } else if (ct == AppConst.INCHING_SHAPEVIEW_TYPE || ct == AppConst.INCHING_TEXTVIEW_TYPE) {
            GsonBuilder builder = new GsonBuilder();
            //gson转对象
            builder.registerTypeAdapter(InchingOutLineAllTypeV10Bean.class, new InchingOutLineAnalysisRules());
            gson = builder.create();
            InchingOutLineTextOrShapeBean inchingTextsContourShapeOrTextBean = gson.fromJson(json, InchingOutLineTextOrShapeBean.class);
            return inchingTextsContourShapeOrTextBean;
        } else if (ct == AppConst.INCHING_TABLEVIEW_TYPE) {
            InchingOutLineTableBean inchingTableDataBean = gson.fromJson(json, InchingOutLineTableBean.class);
            return inchingTableDataBean;
        }
        return null;
    }
}
